#include <dspSimulator/instruction/andn.h>

bool ANDN::checkCondition(std::shared_ptr<DspMachine> dspMachine) {
    return checkStandardCondition(dspMachine);
}

void ANDN::loadInstIRIntoJit(llvm::LLVMContext *llvmContext, llvm::Module *module,
                             std::shared_ptr<llvm::IRBuilder<>> irBuilder) {
    llvm::FunctionType *funcType = llvm::FunctionType::get(irBuilder->getVoidTy(), false);

    // --------------------------------- ANDN unit op1,xop2,dst ---------------------------------------------
    {
        llvm::Function *andL = llvm::Function::Create(funcType, llvm::GlobalValue::ExternalLinkage, "ANDNKind1",
                                                      module);
        llvm::BasicBlock *entryBB = llvm::BasicBlock::Create(*llvmContext, "entry", andL);
        irBuilder->SetInsertPoint(entryBB);

        auto src2RegPtr = getSrc2LoRegPtr(module, irBuilder);
        auto src2Reg = irBuilder->CreateLoad(src2RegPtr);
        auto negSrc2Reg = irBuilder->CreateXor(src2Reg, irBuilder->getInt32(0xffffffff));

        auto src1RegPtr = getSrc1LoRegPtr(module, irBuilder);
        auto src1Reg = irBuilder->CreateLoad(src1RegPtr);

        auto andVal = irBuilder->CreateAnd(src1Reg, negSrc2Reg);

        auto dstRegPtr = getDstLoRegPtr(module, irBuilder);

        irBuilder->CreateStore(andVal, dstRegPtr);
        irBuilder->CreateRetVoid();
    }
}

std::shared_ptr<Instruction> ANDN::decode(std::shared_ptr<InstInfo> instInfo, u64 baseCycle) {
    std::shared_ptr<ANDN> andn;
    auto dspInstInfo = std::dynamic_pointer_cast<DspInstInfo>(instInfo);
    bool isStandardInst = dspInstInfo->isStandardInstruction();
    if (isStandardInst) {
        auto data = vectorToBitSet<32>(dspInstInfo->getBits());
        if (checkBits<10>(data, 2, 0b1111100110) && existStandardCondition(data)) {
            andn = std::make_shared<ANDN>(dspInstInfo->getAddress(), baseCycle);
            andn->creg_z = extractBits<4>(data, 28);
            andn->dst = extractBits<5>(data, 23).to_ulong();
            andn->src2 = extractBits<5>(data, 18).to_ulong();
            andn->src1 = extractBits<5>(data, 13).to_ulong();
            andn->x = data[12];
            andn->s = data[1];
            andn->p = data[0];
            if (andn->s == 0) {
                andn->funcUnit = FuncUnit::L1;
            } else {
                andn->funcUnit = FuncUnit::L2;
            }
            andn->instFormat = InstFormat::Kind1;
        } else if (checkBits<10>(data, 2, 0b1101101100) && existStandardCondition(data)) {
            andn = std::make_shared<ANDN>(dspInstInfo->getAddress(), baseCycle);
            andn->creg_z = extractBits<4>(data, 28);
            andn->dst = extractBits<5>(data, 23).to_ulong();
            andn->src2 = extractBits<5>(data, 18).to_ulong();
            andn->src1 = extractBits<5>(data, 13).to_ulong();
            andn->x = data[12];
            andn->s = data[1];
            andn->p = data[0];
            if (andn->s == 0) {
                andn->funcUnit = FuncUnit::S1;
            } else {
                andn->funcUnit = FuncUnit::S2;
            }
            andn->instFormat = InstFormat::Kind1;
        } else if (checkBits<2>(data, 10, 0b10) && checkBits<8>(data, 2, 0b00001100) &&
                existStandardCondition(data)) {
            andn = std::make_shared<ANDN>(dspInstInfo->getAddress(), baseCycle);
            andn->creg_z = extractBits<4>(data, 28);
            andn->dst = extractBits<5>(data, 23).to_ulong();
            andn->src2 = extractBits<5>(data, 18).to_ulong();
            andn->src1 = extractBits<5>(data, 13).to_ulong();
            andn->x = data[12];
            andn->s = data[1];
            andn->p = data[0];
            if (andn->s == 0) {
                andn->funcUnit = FuncUnit::D1;
            } else {
                andn->funcUnit = FuncUnit::D2;
            }
            andn->instFormat = InstFormat::Kind1;
        }
    }
    return andn;
}

void ANDN::executeCustom(std::shared_ptr<TargetMachine> targetMachine, ExecutePhase executePhase) {

}

std::string ANDN::toString() const {
    std::string res;
    if (!isCompactInstruction) {
        res += getStandardConditionString() + " ";
    }
    res += m_name + " " + FuncUnitName[static_cast<u32>(funcUnit)] + " ";
    if (instFormat == InstFormat::Kind1) {
        res += getSrc1Reg32Name() + "," + getSrc2Reg32Name() + "," + getDstReg32Name();
    }
    return res;
}

std::shared_ptr<DspInstruction> ANDN::clone() {
    return std::make_shared<ANDN>(*this);
}
